package com.bdzl.admin.controller.data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.bdzl.admin.service.feign.FeignFddCaPersonaService;
import com.bdzl.pub.controller.BaseController;
import org.checkerframework.checker.units.qual.A;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.bdzl.pub.entity.data.FddCaPersonalDO;
import com.bdzl.admin.service.data.FddCaPersonalService;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;

/**
 * 法大大个人 CA 证书信息
 * 
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 13:57:43
 */
 
@Controller
@RequestMapping("/bdzl/fddCaPersonal")
@CrossOrigin("*") //处理跨域请求
public class FddCaPersonalController extends BaseController {
	@Autowired
	private FddCaPersonalService fddCaPersonalService;
	@Autowired
	private FeignFddCaPersonaService feignFddCaPersonaService;
	
	@GetMapping()
	String FddCaPersonal(){
	    return "bdzl/fddCaPersonal/fddCaPersonal";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<FddCaPersonalDO> fddCaPersonalList = fddCaPersonalService.list(query);
		int total = fddCaPersonalService.count(query);
		PageUtils pageUtils = new PageUtils(fddCaPersonalList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "bdzl/fddCaPersonal/add";
	}

	@GetMapping("/edit/{capId}")
	String edit(@PathVariable("capId") Integer capId,Model model){
		FddCaPersonalDO fddCaPersonal = fddCaPersonalService.get(capId);
		model.addAttribute("fddCaPersonal", fddCaPersonal);
	    return "bdzl/fddCaPersonal/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(@RequestBody FddCaPersonalDO fddCaPersonal){
		try {
			if(fddCaPersonalService.save(fddCaPersonal)>0){
				return R.ok();
			}
		} catch (DataAccessException e) {
			return R.error("该信息已经添加过了");
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( FddCaPersonalDO fddCaPersonal){
		fddCaPersonalService.update(fddCaPersonal);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer capId){
		if(fddCaPersonalService.remove(capId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] capIds){
		fddCaPersonalService.batchRemove(capIds);
		return R.ok();
	}

	/**
	 * 个人CA申请接口
	 *  个人CA申请接口
	 * @param name 客户姓名
	 * @param email 客户邮箱
	 * @param card 客户证件号码
	 * @param cardType 证件类型
	0-身份证（默认值）；
	1-护照；
	2-军人身份证；
	6-社会保障卡；
	A-武装警察身份证件；
	B-港澳通行证；
	C-台湾居民来往大陆通行证；
	E-户口簿
	 * @param mobile 客户手机号码
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/CACustomer")
	public R CACustomer(String name , String email,String card ,String cardType,String mobile){
		String response= feignFddCaPersonaService.CACustomer(name,email,card,cardType,mobile);
		HashMap<String,String> hashMap = JSON.parseObject(response, HashMap.class);
		// 客户编号
		String capCustomerId = hashMap.get("customer_id");
		Map map = new HashMap();
		map.put("capCustomerId",capCustomerId);
		List<FddCaPersonalDO> fddCaPersonalDOList = fddCaPersonalService.list(map);
		if(fddCaPersonalDOList==null||fddCaPersonalDOList.size()==0){
			FddCaPersonalDO fddCaPersonalDO = new FddCaPersonalDO();
			fddCaPersonalDO.setCapCustomerId(capCustomerId);
			fddCaPersonalDO.setCapCardId(card);
			fddCaPersonalService.save(fddCaPersonalDO);
		}
		return R.ok(hashMap);
	}


	
}
